package com.bm.controller;

import com.bm.Result.CodeEnum;
import com.bm.Result.Result;
import com.bm.Vo.Login.LoginInfoVo;
import com.bm.Vo.Login.ValidateCodeVo;
import com.bm.model.EjyyPropertyCompanyUser;
import com.bm.query.Login.AccountLoginQuery;
import com.bm.query.init.InitQuery;
import com.bm.service.EjyyPropertyCompanyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 86134
 * @version 1.0
 * @description TODO
 * @date 2024/2/27 20:32
 */
@RestController
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private EjyyPropertyCompanyUserService ejyyPropertyCompanyUserService;

    @GetMapping("/user/captcha")
    @ApiOperation(value = "验证码api")
    public Result UserCaptcha(HttpServletRequest httpServletRequest){
        ValidateCodeVo validateCode = ejyyPropertyCompanyUserService.createValidateCode();
        return Result.OK(validateCode);
    }

    @PostMapping("/user/account_login")
    @ApiOperation(value = "登录接口")
    public Result accountLogin(@RequestBody AccountLoginQuery accountLoginQuery){
        return ejyyPropertyCompanyUserService.login(accountLoginQuery);
    }

    @GetMapping("/user/info")
    @ApiOperation(value = "初始化用户数据")
    public Result loginUserInfo(){
        LoginInfoVo loginInfoVo = ejyyPropertyCompanyUserService.getUserInfo();
        return Result.OK(loginInfoVo);
    }

    @PostMapping("/user/mp_login")
    @ApiOperation(value = "小程序登录")
    public Result MpUserLogin(){
        return null;
    }

    @GetMapping("/user/logout")
    @ApiOperation(value = "平台端退出登录")
    public Result UserLogout(HttpServletRequest httpServletRequest){
        boolean flag = ejyyPropertyCompanyUserService.logout(httpServletRequest);
        return flag ? Result.OK(CodeEnum.USER_LOGOUT) : Result.FAIL(CodeEnum.FAIL);
    }

    @PostMapping("/user/reset")
    @ApiOperation(value = "重置密码")
    public Result UserResetPassword(@RequestBody EjyyPropertyCompanyUser propertyCompanyUser){
        boolean flag = ejyyPropertyCompanyUserService.reset(propertyCompanyUser);
        return flag ? Result.OK() : Result.FAIL();
    }

    @PostMapping("/init/run")
    public Result InitRun(@RequestBody InitQuery initQuery){
        Boolean flag = ejyyPropertyCompanyUserService.initRun(initQuery);
        return flag ? Result.OK(CodeEnum.SUCCESS) : Result.OK(CodeEnum.SYSTEMT_ALREADY_INIT);
    }

}
